/*============================================================================
 PROJECT: Kleii
 FILE:    MKAssetGroupPickerViewController.m
 AUTHOR:  Khoai Nguyen Minh
 DATE:    12/21/13
 =============================================================================*/

/*============================================================================
 IMPORT
 =============================================================================*/
#import "MKAssetGroupPickerViewController.h"
#import "MKAssetsPickerViewController.h"
#import "AssetLibraryHelper.h"

/*============================================================================
 PRIVATE MACRO
 =============================================================================*/


/*============================================================================
 PRIVATE INTERFACE
 =============================================================================*/

@interface MKAssetGroupPickerViewController ()
@property (nonatomic, strong) NSMutableArray *groups;
@property (nonatomic, strong) UILabel *footerLabel;
@end

@implementation MKAssetGroupPickerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /* update title bar */
    self.titleLabel.text = NSLocalizedString(@"Albums", nil);
    
    /* create table view */
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    
    /* remove extra footer */
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 30)];
    self.tableView.tableFooterView = footerView;
    
    self.footerLabel = [[UILabel alloc] initWithFrame:footerView.bounds];
    self.footerLabel.backgroundColor = [UIColor clearColor];
    if(SupportiOS7) {
        self.footerLabel.textAlignment = NSTextAlignmentCenter;
    } else {
        self.footerLabel.textAlignment = UITextAlignmentCenter;
    }
    self.footerLabel.font = [UIFont systemFontOfSize:13];
    self.footerLabel.textColor = [UIColor lightGrayColor];
    [footerView addSubview:self.footerLabel];
    
    /* query groups */
    [[AssetLibraryHelper sharedPhotoLibraryAccessor] getAssetGroupsFromLibrary:^(NSMutableArray *groups, NSError *error) {
        self.groups = groups;
        [self.tableView reloadData];
        
        if(self.groups.count > 0) {
            NSString *title = self.groups.count > 1 ? @"albums" : @"album";
            self.footerLabel.text = [NSString stringWithFormat:@"%d %@", self.groups.count, title];
        } else {
            self.footerLabel.text = nil;
        }
    }];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Button Events

- (void)didTouchedOnLeftButton:(id)sender {
    if(SupportiOS7) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
            
        }];
    } else {
        [self.navigationController dismissModalViewControllerAnimated:YES];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.groups.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"GroupAssetCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.textLabel.font = [UIFont systemFontOfSize:13];
        cell.textLabel.textColor = [UIColor grayColor];
    }
    
    ALAssetsGroup *group = [self.groups objectAtIndex:indexPath.row];
    cell.imageView.image = [group thumnailImage];
    cell.textLabel.text = [group name];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    /* grab selected group and move to detail asset page */
    ALAssetsGroup *selectedGroup = [self.groups objectAtIndex:indexPath.row];
    
    @autoreleasepool {
        MKAssetsPickerViewController *assetPickerViewController = [[MKAssetsPickerViewController alloc] init];
        assetPickerViewController.group = selectedGroup;
        [self.navigationController pushViewController:assetPickerViewController animated:YES];
    }
}

@end
